﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Interop;

namespace Infrastructure.Helpers
{
    public static class WindowHelp
    {
        [DllImport("user32.dll")]
        static extern int GetWindowLong(nint hwnd, int index);

        [DllImport("user32.dll")]
        static extern int SetWindowLong(nint hwnd, int index, int newStyle);

        [DllImport("user32.dll")]
        static extern bool SetWindowPos(nint hwnd, nint hwndInsertAfter,
                   int x, int y, int width, int height, uint flags);

        [DllImport("user32.dll")]
        static extern nint SendMessage(nint hwnd, uint msg,
                   nint wParam, nint lParam);

        const int GWL_EXSTYLE = -20;
        const int WS_EX_DLGMODALFRAME = 0x0001;
        const int SWP_NOSIZE = 0x0001;
        const int SWP_NOMOVE = 0x0002;
        const int SWP_NOZORDER = 0x0004;
        const int SWP_FRAMECHANGED = 0x0020;
        const uint WM_SETICON = 0x0080;

        public static void RemoveIcon(Window window)
        {
            //获取窗体的句柄
            var hwnd = new WindowInteropHelper(window).Handle;

            //改变窗体的样式
            var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
            SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_DLGMODALFRAME);

            //更新窗口的非客户区，以反映变化
            SetWindowPos(hwnd, nint.Zero, 0, 0, 0, 0, SWP_NOMOVE |
                  SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
        }

    }
}
